﻿
@inject IDataCacheService _dataCacheService
@inject IHttpService _httpService
@inject IPopupService PopupService


<MDialog @bind-Value="Dialog"
         Width="500">
    <MDCard>
        <MCardTitle Class="text-h5">
            移动收藏
        </MCardTitle>
        <MCardText>
            在下方选择要移动到的收藏夹，可以选中多个
        </MCardText>
        <MCardText Class="pb-0">
            <div style="display:flex;">
                @foreach (var item in UserFavoriteInfor)
                {
                    <MCheckbox Value="SelectedItemIds.Contains( item.Id)" TValue="bool" ValueChanged="@((e) => { if (e) SelectedItemIds.Add(item.Id); else SelectedItemIds.RemoveAll(s => s == item.Id); })"
                               Label="@item.Name" Class="m-0 me-4"
                               HideDetails="true"
                               />
                }

            </div>

        </MCardText>


        <MCardActions>
            <MSpacer></MSpacer>
            <CnGalWebSite.Components.Buttons.MasaButton Text="移动" IsAsync="true" OnClick="OnMove" Icon="mdi-check" Color="success" TextStyle />
        </MCardActions>
    </MDCard>
</MDialog>
@code {
    [Parameter]
    public EventCallback OnMoved { get; set; }
    [Parameter]
    public long Id { get; set; }

    public List<FavoriteFolderOverviewModel> UserFavoriteInfor { get; set; } = new List<FavoriteFolderOverviewModel>();


    List<long> SelectedItemIds = new List<long>();
    bool Dialog { get; set; }
    List<KeyValuePair<FavoriteObjectType, long>> ObjectIds;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public void Refresh(List<KeyValuePair<FavoriteObjectType, long>> objectIds,List< FavoriteFolderOverviewModel> userFavoriteInfor)
    {
        if(SelectedItemIds.Any())
        {
            SelectedItemIds.Clear();
        }
        UserFavoriteInfor = userFavoriteInfor;
        ObjectIds = objectIds;
        Dialog = true;
        StateHasChanged();
        
    }

    private async Task MoveFavoriteObjects(IEnumerable<long> folderIds, List<KeyValuePair<FavoriteObjectType, long>> objectIds)
    {
        //调用API
        try
        {
            var obj = await _httpService.PostAsync<MoveFavoriteObjectsModel, Result>("api/favorites/MoveFavoriteObjects", new MoveFavoriteObjectsModel { CurrentFolderId = Id, FolderIds = folderIds.ToArray(), ObjectIds = objectIds });

            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("移动收藏失败", obj.Error);
            }
            else
            {

                await PopupService.ToastSuccessAsync("移动收藏成功", "移动收藏成功");
                Dialog = false;
                StateHasChanged();
                await OnMoved.InvokeAsync();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "移动收藏失败");
        }
    }

    public async Task OnMove()
    {
        if (SelectedItemIds.Any()==false)
        {
            await PopupService.ToastErrorAsync("请选择要移动到的收藏夹", "请选择要移动到的收藏夹");
            return;
        }
        await MoveFavoriteObjects(SelectedItemIds, ObjectIds);
    }
}
